<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\bootstrap\ActiveForm;
use yii\widgets\LinkPager;
use yii\grid\GridView;
use yii\helpers\Url;
$this->title = '临时授权';
?>
<?= Html::cssFile('./assets/date/bootstrap-datetimepicker.min.css')?>
<div class="container">
	<div class="row">
		<?php echo $this->render('_leftnav') ?>
		<div class="col-md-10">
			<div class="page-header clearfix">
				<h1 class="pull-left">临时授权</h1>
			</div>
			<div class="pull-right">
					<a class="btn btn-success btn-sm" data-toggle="modal" data-target="#addTemporary">添加临时授权</a>
				</div>
			<?php echo $this->render('_adminnav') ?>
			<table id="user-table" class="table table-striped table-hover" data-search-form="#user-search-form">
				<thead>
					<tr>
						<th>用户名</th>
						<th>Email</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
				<?php if($admin){ ?>
					<?php foreach($admin as $key => $val){ ?>
						<tr id="user-table-tr-14">
							<td>
								<strong>
									<a href="javascript:;"><?= Html::encode($val['username']) ?></a>
								</strong>
							</td>
							<td>
								<?php if($val['email']){
									echo Html::encode($val['email']);
								}else{
									echo "--";	
								}?><br>
							</td>
							<td>
								<div class="btn-group">
									<a href="javascript:;"  class="btn btn-default btn-sm TempAllot" data-toggle="modal" data-target="#TempAllot" type="<?= Html::encode($val['id'])?>">查看当前用户权限</a>
									<a href="javascript:;"  class="btn btn-default btn-sm TempDelete" type="<?= Html::encode($val['id'])?>">收回权限</a>
								</div>
							</td>
						</tr>
					<?php } ?>
				<?php }else{ ?>
					<tr>
						<td colspan="20">
							<div class="empty">暂无授权用户记录</div>
						</td>
					</tr>
				<?php } ?>
				</tbody>
			</table>
		</div>
	</div>
</div>
<div id="addTemporary" class="modal fade">
	<div class="modal-dialog" style="width:900px">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title">添加临时授权</h4>
			</div>
			<div class="modal-body">
			<?php if($auth){ ?>
				<?php 
					$form = ActiveForm::begin([
						'options' => ['class' => 'form-horizontal'],
						'action'=>Url::to(['user/temporarycreate']),
						'method'=>'post',
						'fieldConfig' => [
							'template' => '<div class="form-group"><center><label class="col-md-2 control-label" for="type-name-field">{label}</label></center><div class="col-md-8 controls">{input}{error}</div></div>'
						], 
					]);
				?>
				<table class="table table-striped table-condenseda table-bordered">
					<tbody>
						<?= Html::input('hidden', 'admin_id', $admin_info['id']) ?>
						<tr>
							<th width="15%">当前管理员名称</th>
							<td width="85%"><?= Html::encode($admin_info['username']) ?></td>
						</tr>

						<tr style="valign:middle;">
							<th>可分配的角色</th>
							<td>
								<?php foreach($auth as $key => $val) { ?>
										<li style="width:170px;height:25px;display:block;clear:both;"><?= Html::checkbox('node_id[]', null,['class' => 'che check'.$val['node_id'],'value' => $val['node_id'],'style'=>'clear:both;']) ?>&nbsp;&nbsp;&nbsp;<?= Html::encode($val['node_title']) ?></li>
									<?php if($val['node']) { ?>
										<?php foreach($val['node'] as $k => $v){?>
												<li style="display:block;float:left;margin-left:40px;width:140px;height:25px;"><?= Html::checkbox('node_id[]', null,['class' => 'che'.$val['node_id'].' check','value' => $v['node_id'], 'p_id' =>$val['node_id'] ]) ?>&nbsp;&nbsp;&nbsp;<?= Html::encode($v['node_title']) ?></li>
										<?php }?>
									<?php } ?>
								<?php }?>
							</td>
						</tr>
						
						<tr>
							<th>临时授权名单</th>
							<td>
								<?= Html::dropDownList('admin', null,ArrayHelper::map($admin_data,'id', 'admin'), ['class' => 'form-control admin' ]);?>
							</td>
						</tr>
						
						<tr>
							<th>结束时间</th>
							<td>
								<?= Html::input('text', 'node_temp_expire',null,['class' => 'form-control form-datetime','style' => 'width:23%','id' => 'endDateTime','placeholder' => '结束时间']) ?>
							</td>
						</tr>
					</tbody>
				</table>
			</div>
			<div class="modal-footer">	
				<button type="button" class="btn btn-link" data-dismiss="modal" tabindex="6">取消</button>
				<?= Html::submitButton('保存', ['class' => 'btn btn-success']) ?>
			</div>
			<?php ActiveForm::end(); ?>
			<?php }else{ ?>
				<table class="table table-striped table-condenseda table-bordered">
					<tbody>
						<tr>
							<td colspan="20">
								<div class="empty">当前用户没有任何权限无法实现临时授权</div>
							</td>
						</tr>
					</tbody>
				</table>
			<?php }?>
		</div>
	</div>
</div>
<div class="modal" id="TempAllot"></div>
<?= Html::jsFile('./assets/date/bootstrap-datetimepicker.min.js')?>
<?= Html::jsFile('./assets/date/bootstrap-datetimepicker.fr.js')?>
<script type="text/javascript">
	//全选
	$(document).on("click",".che",function(){
		var val = $(this).val()
		if($(this).prop("checked")==true){
			$(".che"+val).prop("checked",true);
		}else{
			$(".che"+val).prop("checked",false);
		}
	});
	//选择父节点
	$(document).on("click",".check",function(){
		var val = $(this).attr('p_id')
		if($(this).prop("checked")==true){
			$(".check"+val).prop("checked",true);
		}
	});
	//时间
	$('.form-datetime').datetimepicker({
		autoclose: 1,//当选择一个日期之后是否立即关闭此日期时间选择器
		todayHighlight: 1,//如果为true, 高亮当前日期
		startView: 2,//日期时间选择器打开之后首先显示的视图  控制样式  0~4 
	});
	$('#endDateTimes').click(function(){
		alert(123)
	})
	//详情
	$(".TempAllot").click(function(){
		var id = $(this).attr('type');
		$.ajax({
			type: "POST",
			url: "<?php echo Url::to(['user/tempallot']);?>",
			data: "id="+id,
			dataType: 'json',
			success: function(msg){
				if (msg.code == 200)
				{
					$("#TempAllot").html(msg.data);
				}else if(msg.code == 403)
				{
					$("#TempAllot").html('<div class="modal-dialog " style="width:170px;"><div class="modal-content"><div class="modal-header"><button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button><h4 class="modal-title"><font color="red">'+msg.msg+'</font></h4></div></div></div>');
				}
			}
		});
	})
	//收回权限
	$(".TempDelete").click(function(){
		if(confirm("您确定要收回权限吗？")){
			var id = $(this).attr('type');
			$.ajax({
				type: "POST",
				url: "<?php echo Url::to(['user/tempdelete']);?>",
				data: "id="+id,
				dataType: 'json',
				success: function(msg){
					if (msg.code == 200) {
						history.go(0)
					}else if(msg.code == 400)
					{
						alert('删除失败')
					}else if(msg.code == 403) 
					{
						alert('暂无权限')
					}
				}
			});
		}
	})
</script>